Convert base::StringPiece to std::string_view in //components 1/5 The changes of this CL are made using the following script. Script: https://issues.chromium.org/issues/40506050#comment343 Bug: 40506050 Change-Id: I84d00d5e35ae595ecf018c0a7a80cc30d09e01d1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5501048 Commit-Queue: Helmut Januschka <helmut@januschka.com> Auto-Submit: Helmut Januschka <helmut@januschka.com> Reviewed-by: Nico Weber <thakis@chromium.org> Cr-Commit-Position: refs/heads/main@{#1295041} NOKEYCHECK=True GitOrigin-RevId: 26faf71596faafbf3893f795e7554dfe7f0b392d diff --git a/values.cc b/values.cc index 0249820..f36cda8 100644 --- a/values.cc +++ b/values.cc
@@ -6,19 +6,19 @@ #include <new> #include <ostream> +#include <string_view> #include <utility> #include "base/check_op.h" #include "base/notreached.h" #include "base/numerics/safe_conversions.h" -#include "base/strings/string_piece.h" #include "base/strings/string_util.h" #include "components/cbor/constants.h" namespace cbor { // static -Value Value::InvalidUTF8StringValueForTesting(base::StringPiece in_string) { +Value Value::InvalidUTF8StringValueForTesting(std::string_view in_string) { return Value( base::span<const uint8_t>( reinterpret_cast<const uint8_t*>(in_string.data()), in_string.size()), @@ -99,7 +99,7 @@ : type_(Type::BYTE_STRING), bytestring_value_(std::move(in_bytes)) {} Value::Value(const char* in_string, Type type) - : Value(base::StringPiece(in_string), type) {} + : Value(std::string_view(in_string), type) {} Value::Value(std::string&& in_string, Type type) noexcept : type_(type) { switch (type_) { @@ -117,7 +117,7 @@ } } -Value::Value(base::StringPiece in_string, Type type) : type_(type) { +Value::Value(std::string_view in_string, Type type) : type_(type) { switch (type_) { case Type::STRING: new (&string_value_) std::string(); @@ -235,10 +235,10 @@ return bytestring_value_; } -base::StringPiece Value::GetBytestringAsString() const { +std::string_view Value::GetBytestringAsString() const { CHECK(is_bytestring()); const auto& bytestring_value = GetBytestring(); - return base::StringPiece( + return std::string_view( reinterpret_cast<const char*>(bytestring_value.data()), bytestring_value.size()); } diff --git a/values.h b/values.h index 949f2ee..0345fec 100644 --- a/values.h +++ b/values.h
@@ -8,6 +8,7 @@ #include <stdint.h> #include <string> +#include <string_view> #include <tuple> #include <vector> @@ -15,7 +16,6 @@ #include "base/containers/flat_map.h" #include "base/containers/span.h" #include "base/notreached.h" -#include "base/strings/string_piece.h" #include "components/cbor/cbor_export.h" namespace cbor { @@ -120,7 +120,7 @@ // Returns a Value with Type::INVALID_UTF8. This factory method lets tests // encode such a value as a CBOR string. It should never be used outside of // tests since encoding may yield invalid CBOR data. - static Value InvalidUTF8StringValueForTesting(base::StringPiece in_string); + static Value InvalidUTF8StringValueForTesting(std::string_view in_string); Value(Value&& that) noexcept; Value() noexcept; // A NONE value. @@ -140,7 +140,7 @@ explicit Value(const char* in_string, Type type = Type::STRING); explicit Value(std::string&& in_string, Type type = Type::STRING) noexcept; - explicit Value(base::StringPiece in_string, Type type = Type::STRING); + explicit Value(std::string_view in_string, Type type = Type::STRING); explicit Value(const ArrayValue& in_array); explicit Value(ArrayValue&& in_array) noexcept; @@ -188,7 +188,7 @@ const int64_t& GetUnsigned() const; const int64_t& GetNegative() const; const BinaryValue& GetBytestring() const; - base::StringPiece GetBytestringAsString() const; + std::string_view GetBytestringAsString() const; // Returned string may contain NUL characters. const std::string& GetString() const; const ArrayValue& GetArray() const; diff --git a/writer.cc b/writer.cc index f400a65..af894e7 100644 --- a/writer.cc +++ b/writer.cc
@@ -7,12 +7,12 @@ #include <cstdint> #include <ostream> #include <string> +#include <string_view> #include "base/bit_cast.h" #include "base/check_op.h" #include "base/notreached.h" #include "base/numerics/safe_conversions.h" -#include "base/strings/string_piece.h" #include "components/cbor/constants.h" namespace cbor { @@ -92,7 +92,7 @@ } case Value::Type::STRING: { - base::StringPiece string = node.GetString(); + std::string_view string = node.GetString(); StartItem(Value::Type::STRING, base::strict_cast<uint64_t>(string.size())); diff --git a/writer_unittest.cc b/writer_unittest.cc index e3bffe2..ee11e7c 100644 --- a/writer_unittest.cc +++ b/writer_unittest.cc
@@ -7,6 +7,7 @@ #include <cmath> #include <limits> #include <string> +#include <string_view> #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -18,26 +19,26 @@ TEST(CBORWriterTest, TestWriteUint) { struct UintTestCase { const int64_t value; - const base::StringPiece cbor; + const std::string_view cbor; }; static const UintTestCase kUintTestCases[] = { // Reminder: must specify length when creating string pieces // with null bytes, else the string will truncate prematurely. - {0, base::StringPiece("\x00", 1)}, - {1, base::StringPiece("\x01")}, - {10, base::StringPiece("\x0a")}, - {23, base::StringPiece("\x17")}, - {24, base::StringPiece("\x18\x18")}, - {25, base::StringPiece("\x18\x19")}, - {100, base::StringPiece("\x18\x64")}, - {1000, base::StringPiece("\x19\x03\xe8")}, - {1000000, base::StringPiece("\x1a\x00\x0f\x42\x40", 5)}, - {0xFFFFFFFF, base::StringPiece("\x1a\xff\xff\xff\xff")}, + {0, std::string_view("\x00", 1)}, + {1, std::string_view("\x01")}, + {10, std::string_view("\x0a")}, + {23, std::string_view("\x17")}, + {24, std::string_view("\x18\x18")}, + {25, std::string_view("\x18\x19")}, + {100, std::string_view("\x18\x64")}, + {1000, std::string_view("\x19\x03\xe8")}, + {1000000, std::string_view("\x1a\x00\x0f\x42\x40", 5)}, + {0xFFFFFFFF, std::string_view("\x1a\xff\xff\xff\xff")}, {0x100000000, - base::StringPiece("\x1b\x00\x00\x00\x01\x00\x00\x00\x00", 9)}, + std::string_view("\x1b\x00\x00\x00\x01\x00\x00\x00\x00", 9)}, {std::numeric_limits<int64_t>::max(), - base::StringPiece("\x1b\x7f\xff\xff\xff\xff\xff\xff\xff")}}; + std::string_view("\x1b\x7f\xff\xff\xff\xff\xff\xff\xff")}}; for (const UintTestCase& test_case : kUintTestCases) { auto cbor = Writer::Write(Value(test_case.value)); @@ -49,20 +50,20 @@ TEST(CBORWriterTest, TestWriteNegativeInteger) { static const struct { const int64_t negative_int; - const base::StringPiece cbor; + const std::string_view cbor; } kNegativeIntTestCases[] = { - {-1LL, base::StringPiece("\x20")}, - {-10LL, base::StringPiece("\x29")}, - {-23LL, base::StringPiece("\x36")}, - {-24LL, base::StringPiece("\x37")}, - {-25LL, base::StringPiece("\x38\x18")}, - {-100LL, base::StringPiece("\x38\x63")}, - {-1000LL, base::StringPiece("\x39\x03\xe7")}, - {-4294967296LL, base::StringPiece("\x3a\xff\xff\xff\xff")}, + {-1LL, std::string_view("\x20")}, + {-10LL, std::string_view("\x29")}, + {-23LL, std::string_view("\x36")}, + {-24LL, std::string_view("\x37")}, + {-25LL, std::string_view("\x38\x18")}, + {-100LL, std::string_view("\x38\x63")}, + {-1000LL, std::string_view("\x39\x03\xe7")}, + {-4294967296LL, std::string_view("\x3a\xff\xff\xff\xff")}, {-4294967297LL, - base::StringPiece("\x3b\x00\x00\x00\x01\x00\x00\x00\x00", 9)}, + std::string_view("\x3b\x00\x00\x00\x01\x00\x00\x00\x00", 9)}, {std::numeric_limits<int64_t>::min(), - base::StringPiece("\x3b\x7f\xff\xff\xff\xff\xff\xff\xff")}, + std::string_view("\x3b\x7f\xff\xff\xff\xff\xff\xff\xff")}, }; for (const auto& test_case : kNegativeIntTestCases) { @@ -78,12 +79,12 @@ TEST(CBORWriterTest, TestWriteBytes) { struct BytesTestCase { const std::vector<uint8_t> bytes; - const base::StringPiece cbor; + const std::string_view cbor; }; static const BytesTestCase kBytesTestCases[] = { - {{}, base::StringPiece("\x40")}, - {{0x01, 0x02, 0x03, 0x04}, base::StringPiece("\x44\x01\x02\x03\x04")}, + {{}, std::string_view("\x40")}, + {{0x01, 0x02, 0x03, 0x04}, std::string_view("\x44\x01\x02\x03\x04")}, }; for (const BytesTestCase& test_case : kBytesTestCases) { @@ -96,17 +97,17 @@ TEST(CBORWriterTest, TestWriteString) { struct StringTestCase { const std::string string; - const base::StringPiece cbor; + const std::string_view cbor; }; static const StringTestCase kStringTestCases[] = { - {"", base::StringPiece("\x60")}, - {"a", base::StringPiece("\x61\x61")}, - {"IETF", base::StringPiece("\x64\x49\x45\x54\x46")}, - {"\"\\", base::StringPiece("\x62\x22\x5c")}, - {"\xc3\xbc", base::StringPiece("\x62\xc3\xbc")}, - {"\xe6\xb0\xb4", base::StringPiece("\x63\xe6\xb0\xb4")}, - {"\xf0\x90\x85\x91", base::StringPiece("\x64\xf0\x90\x85\x91")}}; + {"", std::string_view("\x60")}, + {"a", std::string_view("\x61\x61")}, + {"IETF", std::string_view("\x64\x49\x45\x54\x46")}, + {"\"\\", std::string_view("\x62\x22\x5c")}, + {"\xc3\xbc", std::string_view("\x62\xc3\xbc")}, + {"\xe6\xb0\xb4", std::string_view("\x63\xe6\xb0\xb4")}, + {"\xf0\x90\x85\x91", std::string_view("\x64\xf0\x90\x85\x91")}}; for (const StringTestCase& test_case : kStringTestCases) { SCOPED_TRACE(testing::Message() @@ -367,12 +368,12 @@ TEST(CBORWriterTest, TestWriteSimpleValue) { static const struct { Value::SimpleValue simple_value; - const base::StringPiece cbor; + const std::string_view cbor; } kSimpleTestCase[] = { - {Value::SimpleValue::FALSE_VALUE, base::StringPiece("\xf4")}, - {Value::SimpleValue::TRUE_VALUE, base::StringPiece("\xf5")}, - {Value::SimpleValue::NULL_VALUE, base::StringPiece("\xf6")}, - {Value::SimpleValue::UNDEFINED, base::StringPiece("\xf7")}}; + {Value::SimpleValue::FALSE_VALUE, std::string_view("\xf4")}, + {Value::SimpleValue::TRUE_VALUE, std::string_view("\xf5")}, + {Value::SimpleValue::NULL_VALUE, std::string_view("\xf6")}, + {Value::SimpleValue::UNDEFINED, std::string_view("\xf7")}}; for (const auto& test_case : kSimpleTestCase) { auto cbor = Writer::Write(Value(test_case.simple_value));